
var replyid = -1;
var replytitle = "";

/*
$(document).ready( 
		function()
		{
			$("#bt_close").click( function(){ $.unblockUI(); } );
			$("#bt_reply").click( function(){
				
				if( replyid == 0 )
				{
					MessageBox('不能向系统管理员回复信件!');
					return;
				}
				
				$("#bt_close").click( function(){ $.unblockUI(); } );
				$("#bt_send_reply").click( SendReply );
				$("#replytitle").val( replytitle );
				$("#replycontent").val("");
				
				ShowModelDialog("#div_reply");
			});
		}
	
);
*/
function SendReply()
{
	params = "content=" + $("#replycontent").val() + "&id=" + replyid + "&title=" + $("#replytitle").val();
	
	$.ajax(
		  	{
		  		type: 'post',
		        url: edp_domain_url + 'ajax_reply_mail/?tiny=2',
		        data: params,
				timeout: 5000,
				global:false,
			    beforeSend: function()
			    {
		  			SimpleMessage( '正在发送站内信...' );
				},
				error: function()
				{
					SimpleMessage( '发送失败，稍后再试...' );
				},
				
				success: function( data, textStatus )
		        {
					
					if( data == "0")
						SimpleMessage( '发送成功！', 1000 );
					else
						MessageBox( data );
				}
		       
	});

}

function RemoveSelectedLetter()
{
	var selectids = "";
	var letters = $("#letterzone > input[type='checkbox']");
	
	for( i = 0; i < letters.length; i++ ) {
		if( letters[i].checked == true ) {
			$(letters[i]).parents().filter(".mail_item").remove();
			decreaseLetterCount( letters[i] );
		}
	}

}

function LabelSelectedLetter( read_ )
{
	var selectids = "";
	var letters = $("#letterzone > input[type='checkbox']");
	var letterCount = parseInt( $("#letterCount").text() );
	var letterDelta = 0;
	for( i = 0; i < letters.length; i++ ) {
		if( letters[i].checked == true )
		{
			if( read_ == 0){
				if( $(letters[i]).parent().attr("class") == 'title_1_2' ){
					$(letters[i]).parent().removeClass('title_1_2');
					$(letters[i]).parent().addClass('title_1');
					letterDelta++;
				}
			} else {
				if( $(letters[i]).parent().attr("class") == 'title_1' ){
					$(letters[i]).parent().removeClass('title_1');
					$(letters[i]).parent().addClass('title_1_2');
					letterDelta--;
				}
			}
		}
	}
	
	$("#letterCount").text( letterCount + letterDelta );

}

function SelectAllLetters( this_ )
{
	if( this_.checked == true )
		$("#letterzone > input[type='checkbox']").attr( "checked", "true");
	else
		$("#letterzone > input[type='checkbox']").removeAttr( "checked");
	
}


function GetSelectedLetter()
{
	var selectids = "";
	var letters = $("#letterzone > input[type='checkbox']");
	
	for( i = 0; i < letters.length; i++ )
		if( letters[i].checked == true )
			selectids += letters[i].value + ",";

	return selectids;
}

function send_del_letter_request()
{
	MessageBox( "确定要删除吗?","删除确认",
			{
				yes: function(){
					  var params = "ids=" + GetSelectedLetter() ;
					  $.ajax(
				    	{
				                type: 'post',
				                url: edp_domain_url + 'ajax_del_letter/?tiny=1',
				                data: params,
								timeout: 5000,
								global:false,
					          
								error: function(XMLHttpRequest, textStatus, errorThrown)
								{
				    				SimpleMessage( '发送请求失败，稍后再试...', 2000 );
								},
				                success: function( data, textStatus )
				                {
									SimpleMessage( data, 500);
									RemoveSelectedLetter();
				                }
				         });}
			}
	);
	
}

function send_lable_letter_request( read_ )
{
	
	  var params = "ids=" + GetSelectedLetter() + "&label=" + read_;
	  $.ajax(
    	{
                type: 'post',
                url: edp_domain_url + 'ajax_label_letter/?tiny=1',
                data: params,
				timeout: 5000,
				global:false,
	          
				error: function(XMLHttpRequest, textStatus, errorThrown)
				{
    				SimpleMessage( '发送请求失败，稍后再试...', 1000 );
				},
                success: function( data, textStatus )
                {
					SimpleMessage( data, 1000 );
					LabelSelectedLetter( read_ );
					
                }
         });
      
	
}

function reply_content(id_,sender_id_ )
{

	var item_title= 'item_title_'+id_;
	replyid = sender_id_;
	
	if( replyid == 0 )
	{
		MessageBox('不能向系统管理员回复信件!');
		return;
	}
	
	

	$("#bt_close").click( function(){ $.unblockUI(); } );
	$("#bt_send_reply").click( SendReply );
	$("#replytitle").val( "RE:" + $("#"+item_title).text().trim() );
	$("#replycontent").val("");
	
	ShowModelDialog("#div_reply");
	
}


function close_content( id_ )
{
	var item_content= 'item_content_'+id_;
	$("#"+item_content).hide();	
}

function show_content1( this_, id_, sender_id_ )
{
	var params = "id=" + id_;
	//replyid = sender_id_;
	//replytitle = "RE:" + $(this_).text().trim();			
	//$("#replyto").val( $(this_).nextAll(".author").text() );
	
	
	
	var item_content= 'item_content_'+id_;

	if( $("#"+item_content).is(':visible') ){
	
 		$("#"+item_content).hide();	
	}
	else{
		if( $("#" + item_content).find('.content_1').text().trim() != '' ) {
			$("#"+item_content).show();
			decreaseLetterCount( this_ );
			return;
		}
		$.ajax({
	                type: 'post',
	                url: edp_domain_url + 'ajax_show_letter/?tiny=1',
	                data: params,
					timeout: 5000,
					global:false,
		          
					error: function(XMLHttpRequest, textStatus, errorThrown)
					{
			    		$.blockUI(
	    						{
	    							message: '发送请求失败，稍后再试...',
	    							css: { padding:'35px',color:'#CC0000'}
	    						});
	    				setTimeout($.unblockUI, 2000); 
					},
	                success: function( data, textStatus )
	                {
	            	                 		                 		            		
	                 	$("#"+item_content+">div[class='content_1']").html(data);		                 		                 			                 		
	                 	$("#"+item_content).show();	
	                 	decreaseLetterCount( this_ );
	                }
			   });
	}	         
	
	
	
}

function decreaseLetterCount( this_ )
{
	if( $(this_).parent().attr('class') == 'title_1' ){
 		$(this_).parent().removeClass('title_1');
    	$(this_).parent().addClass('title_1_2');//mark as read
    	var letterCount = parseInt( $("#letterCount").text() );
    	$("#letterCount").text( --letterCount );
 	}
}

function del_item(id_, this_)
{
	MessageBox( "确定要删除吗?","删除确认",
			{
				yes: function(){
					  var params = "ids=" + id_;
					  $.ajax(
					   	{
					               type: 'post',
					               url: edp_domain_url + 'ajax_del_letter/?tiny=1',
					               data: params,
								timeout: 5000,
								global:false,
					          
								error: function(XMLHttpRequest, textStatus, errorThrown)
								{
					   		
					   				$.blockUI(
					   						{
					   							message: '发送请求失败，稍后再试...',
					   							css: { padding:'35px',color:'#CC0000'}
					   						});
					   				setTimeout($.unblockUI, 2000); 
					   				
								},
					               success: function( data, textStatus )
					               {
									
										SimpleMessage( data, 500 );
										
										$(this_).parents().filter(".mail_item").remove();
										decreaseLetterCount( this_ );
									
					               }
					        });
						}
			}
	);
      
	
}
